package tgc.edu.zz.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import tgc.edu.zz.service.MessageBoardService;
import tgc.edu.zz.vo.MessageBoardVo;

@Controller
@RequestMapping("/messageBoard")
public class MessageBoardController {

	@Autowired
	private MessageBoardService messageService;
	
	/**
	 * 
	 * @param map
	 * @return 留言板页面
	 */
	@RequestMapping("/message")
	public String message(ModelMap map) {
		Sort sort=new Sort(Direction.DESC,"time");
		List<MessageBoardVo> model = messageService.findAll(sort);
		map.put("model", model);
		return "messageBoard/message";
	}
	
	/**
	 * 保存用户提交的留言
	 * @param model
	 * @return 留言板页面
	 */
	@RequestMapping("/save")
	public String save(MessageBoardVo model) {
		MessageBoardVo mo = new MessageBoardVo();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		mo.setTime(df.format(new Date()));
		mo.setName(model.getName());
		mo.setQq(model.getQq());
		mo.setText(model.getText());
		messageService.save(mo);
		return "redirect:message";
	}
	
	
	
}
